using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using MyFrame.Mono;
using Platform.CharacterData;
using Platform.Skill.Deployer;
using UnityEngine;
using Plugin.Tool;

namespace GardeningSkeleton.Platform.SkillSpace
{
    public class SpinImpact : BaseImpactEffect
    {

        ParabolicSkillDeployer deployer;
        private bool wasStopped;
        private float spinDirection;


        public override void EndEffect()
        {
            // ZTimer.ClearTimer(AttackCheck);
            // deployer.anim.SetBool("Rotation", false);
            deployer.AttackEnd();
            ZTimer.ClearTimer(recycle);
        }

        public override void Execute(SkillDeployer baseDeployer)
        {
            deployer = (ThrowSwordSkillDeployer)baseDeployer;

            // x轴一个随机移动方向
            spinDirection = Mathf.Clamp(deployer.rb.velocity.x, -1, 1);
            MonoMgr.Instance.AddUpdateEvent(SpinUpdate);
            deployer.anim.SetBool("Rotation", true);

            // ZTimer.SetInterval(skillData.attackInterval, AttackCheck);
        }

        // private void AttackCheck()
        // {
        //     // 范围检查
        //     deployer.chooseTargets();
        //     if (skillData.attackTargets.Count > 0)
        //     {
        //         foreach (var entity in skillData.attackTargets)
        //         {
        //             deployer.AttackStart();
        //         }
        //     }
        // }

        public override void OnImpactTriggerEntry(Collider2D other)
        {
            deployer.chooseTargets();
            deployer.AttackStart();
            //命中地板或者敌人才停下
            if (other.gameObject.layer == LayerMask.NameToLayer("Enemy"))
                StopWhenSpining();
            else if (other.gameObject.layer == LayerMask.NameToLayer("Ground"))
            {
                // 遇到墙反弹
                spinDirection = -spinDirection;
                StopWhenSpining();
            }
        }

        public override void OnImpactTriggerExit(Collider2D other)
        {
            skillData.attackTargets.Remove(other.transform);
            if (skillData.attackTargets.Count == 0)
                deployer.AttackEnd();
        }

        private void SpinUpdate()
        {
            if (Vector2.Distance(deployer.transform.position, deployer.player.transform.position) > skillData.Distance)
            {
                StopWhenSpining();
            }
            else
            {
                deployer.transform.position = Vector2.MoveTowards(deployer.transform.position,
               new Vector2(deployer.transform.position.x + spinDirection, deployer.transform.position.y),
               1.5f * Time.deltaTime);
            }
        }

        private void StopWhenSpining()
        {
            if (wasStopped) return;
            wasStopped = true;
            deployer.rb.constraints = RigidbodyConstraints2D.FreezeAll;
            // 不加可能有抖动
            deployer.SetRotate(false);

            //一段时间后回收
            ZTimer.ClearTimer(recycle);
            ZTimer.SetTimeout(skillData.durationTime, recycle);
        }

        private void recycle()
        {
            deployer.ReturnSword();
            deployer.SetRotate(true);
            wasStopped = false;
            MonoMgr.Instance.RemoveUpdateEvent(SpinUpdate);
        }
    }
}